﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace Ratatoskr.Toolbox
{
    class StrictSquareCell : Cell
    {
        public static new int IntersectRange
        {
            get { return 2; }
        }

        public StrictSquareCell(ParentEntity parent, int xLoc, int yLoc)
            : base(xLoc, yLoc, 4) { }

        protected internal override void FindAdjacentCells()
        {
            Grid<StrictSquareCell> grid = _parent as Grid<StrictSquareCell>;
            if (grid == null)
                throw new ArgumentNullException("Cannot find adjacent cells without a grid parent.");
            _adjacent = new Cell[4];
            if (_yLoc != grid.MinExtent.Y)
                _adjacent[0] = grid[_xLoc, _yLoc - 1];
            if (_xLoc != grid.MaxExtent.X)
                _adjacent[1] = grid[_xLoc + 1, _yLoc];
            if (_yLoc != grid.MaxExtent.Y)
                _adjacent[2] = grid[_xLoc, _yLoc + 1];
            if (_xLoc != grid.MinExtent.X)
                _adjacent[3] = grid[_xLoc - 1, _yLoc];
        }

        protected internal override CellPosition _CellStep(float x, float y)
        {
            Cell c = this;
            float x2 = x;
            float y2 = y;
            if (Math.Abs(x) > Math.Abs(y))
            {
                if (x > 0.5)
                {
                    x2 -= 1;
                    c = _adjacent[0];
                }
                else if (x < -0.5)
                {
                    x2 += 1;
                    c = _adjacent[2];
                }
            }
            else if (y > 0.5)
            {
                y2 -= 1;
                c = _adjacent[1];
            }
            else if (y < -0.5)
            {
                y2 += 1;
                c = _adjacent[3];
            }
            if (c == null)
                return new CellPosition(this, x, y, true);
            return new CellPosition(c, x, y);
        }
    }
}

